package com.kasax.android.arch.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import com.kasax.android.arch.base.app.BaseApp;

public class UIUtils {
    public static void showShortToast(String msg) {
        safeShowToast(msg, Toast.LENGTH_SHORT);
    }

    public static void showLongToast(String msg) {
        safeShowToast(msg, Toast.LENGTH_LONG);
    }

    public static void safeShowToast(String msg, int duration) {
        if (isInMainThread()) {
            Toast.makeText(getAppContext(), msg, duration).show();
        } else {
            getAppHandler().post(() -> Toast.makeText(getAppContext(), msg, duration).show());
        }
    }

    public static Handler getAppHandler() {
        return BaseApp.getAppHandler();
    }

    public static Context getAppContext() {
        return BaseApp.getContext();
    }

    public static boolean isInMainThread() {
        return Looper.myLooper() == Looper.getMainLooper();
    }
}
